package com.maze.game.view
{
	import com.maze.app.util.GlideTransition;
	import com.maze.app.util.TransitionView;
	import com.maze.game.control.Alert;
	import com.maze.game.event.GameEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class MainView extends Sprite
	{
		private var menuView:MenuView;
		
		private var gameView:GameView;
		
		private var gameTestView:GameTestView;
		
		private var prevView:Sprite;
		
		public function MainView()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE, initStage);
		}
		
		private function initStage(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initStage);
			goMenu();
		}
		
		private function goMenu():void
		{
			menuView = new MenuView();
			addChild(menuView);
			var menuEvent:Function;
			menuView.addEventListener(GameEvent.PLAY_GAME, menuEvent = function(evt:Event):void{
				menuView.removeEventListener(GameEvent.PLAY_GAME, menuEvent);
				prevView = menuView;
				goGame();
			});
			menuView.addEventListener(GameEvent.MAZE_TEST, menuEvent = function(evt:Event):void{
				menuView.removeEventListener(GameEvent.MAZE_TEST, menuEvent);
				prevView = menuView;
				goGameTest();
			});
			if(prevView)
				GlideTransition.start(prevView,menuView,true);
		}
		
		private function goGame():void
		{
			gameView = new GameView();
			addChild(gameView);
			var quitFnc:Function;
			gameView.addEventListener(GameEvent.QUIT_GAME, quitFnc = function(evt:GameEvent):void{
				gameView.removeEventListener(GameEvent.QUIT_GAME, quitFnc);
				prevView = gameView;
				goMenu();
			});
			GlideTransition.start(prevView,gameView);
		}
		
		private function goGameTest():void
		{
			gameTestView = new GameTestView();
			addChild(gameTestView);
			var quitFnc:Function;
			gameTestView.addEventListener(GameEvent.QUIT_GAME, quitFnc = function(evt:GameEvent):void{
				gameTestView.removeEventListener(GameEvent.QUIT_GAME, quitFnc);
				prevView = gameTestView;
				goMenu();
			});
			GlideTransition.start(prevView,gameTestView);
		}
	}
}